package com.kj.mynote.Util;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author wy
 * @version V1.0
 * @Title: MyApplication
 * @Package com.kj.mynote
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 17/3/22 13:44
 */

public class ToolUtils {
    /**
     * 时间戳转String
     *
     * @param time
     * @return
     */
    public static String TS2S(long time, String f) {
        SimpleDateFormat format = new SimpleDateFormat(f);
        String d = format.format(time);
        return d;
    }
    public static long S2TS(String time,String f){
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat(f);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 计算两个时间戳的间隔
     *
     * @return
     */
    public static String TS2TS(long sTime, long eTime) {
        Date st = new Date(sTime);
        Date et = new Date(eTime);
        long dd1 = st.getTime();
        long dd2 = et.getTime();
        int h = (int) (dd2 - dd1) / 3600 / 1000;
        int m = (int) (dd2 - dd1 - h * 3600 * 1000) / 60 / 1000;
        if (h == 0) {
            return m + "分钟";
        } else {
            return h + "小时" + m + "分钟";
        }
    }

    /**
     * 计算sTime间隔day的时间
     * @param sTime
     * @param day
     * @return
     */
    public static long TSADD(long sTime,int day){
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(sTime);
        calendar.add(calendar.DATE,day);
        return calendar.getTimeInMillis();
    }
}
